<html>

<head>
  <title></title>
</head>

<body>

	<form method=post>

    <center>
    <table border=1 width=50% cellpadding=10>
    <tr>
    	<td>
        	Tag - plane(planeid)
        </td>
    	<td>
        	Sorties (allsorties)
        </td>
    	<td>
        	Curpilots (maxpilots)
        </td>
    	<td>
        	Check 2 drop
        </td>
    </tr>
<?php

	include "kernel.php";
	include "adw_functions.php";

    if (!$_POST[squad]) {



	    $r = mysql_query("select * from squadrons order by id");


	    while ($res = mysql_fetch_assoc($r)) {

				$plane = GetSquadPlane($res[id]);
				$plname = GetPlaneNameById($plane);

				echo "<tr><td align=center>$res[tag] - [$plname ($plane)]</td>
						<td align=center>$res[sorties] ($res[allsorties])</td>
						<td align=center>$res[curpilots] ($res[maxpilots])</td>
						<td align=center><input type=checkbox name=squad[$res[id]]";


				if ($res[sorties]==0) echo " checked";

				echo "></td></tr>";
	    }

		echo "<tr><td colspan=4 align=center><input type=submit name=go value=\" Drop \"></td></tr>";

    }
    else {


	 	$squads = array_keys($_POST[squad]);

        foreach ($squads as $sq=>$value) {

	            $sq = GetSquadById($value);


	            $plane = GetSquadPlane($sq[id]);

			$cid = GetActiveCompany();
			$cid = $cid[id];


			$curplanes = GetSquadPlanesIds($sq[id]);

			$curline = $sq[lineid];

			if (is_array($curplanes))
			{
				foreach ($curplanes as $planeid=>$planecount)
				{
					mysql_query("update reserv set busycount=busycount-$planecount where plane_id='".$planeid."' and company_id=$cid and army='$sq[army]' and typeOf='$sq[typeOf]'");
					echo "update reserv set busycount=busycount-$planecount where plane_id='".$planeid."' and company_id=$cid and army='$sq[army]' and typeOf='$sq[typeOf]\n<br>";
        		}
			}
			else
			{	            mysql_query("update reserv set busycount=busycount-$sq[maxpilots] where army='$sq[army]' and typeOf='$sq[typeOf]' and plane_id=$plane and company_id=$cid");
				echo "update reserv set busycount=busycount-$sq[maxpilots] where army='$sq[army]' and typeOf='$sq[typeOf]' and plane_id=$plane and company_id=$cid";
			}

	        mysql_query("update squadrons set isactive=0, lineid=0, freezeline=0 where id='$sq[id]'");
            mysql_query("delete from planesets where squad_id='$sq[id]'");

            echo "<h2>Line for squad $sq[tag] was dropped.</h2>";
	        echo "<h2>Done.</h2>";

        }

    }

?>

</form>
	</table>
    </center>

</body>

</html>